//
//  WBWelcomeView.swift
//  新浪微博
//
//  Created by zj on 2017/10/21.
//  Copyright © 2017年 zj. All rights reserved.
//

import UIKit
import SDWebImage

class WBWelcomeView: UIView {

    @IBOutlet weak var iconView: UIImageView!
    @IBOutlet weak var tipLabel: UILabel!
    @IBOutlet weak var bottomCons: NSLayoutConstraint!
    
    class func welcomeView() -> WBWelcomeView {
        let  nib = UINib.init(nibName: "WBWelcomeView", bundle: nil)
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! WBWelcomeView
        
        v.frame = UIScreen.main.bounds
        
        return v
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        // 提示：initWithCode 只是刚刚从 XIB 的二进制文件将视图数据加载完成
        // 还没有和代码连线建立起关系，所以开发时，千万不要在这个方法中处理 UI
    }
    
    override func awakeFromNib() {
        
        guard let urlString = WBNetworkManager.shared.userAccount.avatar_large,
        let url = URL.init(string: urlString) else {
            return
        }
        
        iconView.sd_setImage(with: url, placeholderImage: UIImage.init(named: "avatar_default_big"))
        
        // 设置圆角
        iconView.layer.cornerRadius = iconView.bounds.width * 0.5
        iconView.layer.masksToBounds = true
    }
    
    override func didMoveToWindow() {
        super.didMoveToWindow()
        // 
        self.layoutIfNeeded()
        
        bottomCons.constant = bounds.size.height - 200
        UIView.animate(withDuration: 1.0,
                       delay: 0,
                       usingSpringWithDamping: 0.7,
                       initialSpringVelocity: 0,
                       options: [],
                       animations: {
                        self.layoutIfNeeded()
        }) { (_) in
            UIView.animate(withDuration: 1.0, animations: {
                self.tipLabel.alpha = 1
            }, completion: { (_) in
                self.removeFromSuperview()
            })
        }
    }
}
